package com.ctsi.offlinesupport.service;


import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.offlinesupport.dto.SupportInfoDTO;
import com.ctsi.supportflow.app.entity.SupportInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SupportInfo Service 类
 * @author   liuhx
 * @description  SupportInfo
 * @created	 Mon Oct 01 17:49:34 CST 2018
 */
@Service
public class SupportInfoService {
	private static final Logger logger=LoggerFactory.getLogger(SupportInfoService.class);

	@Autowired
	private MybatisGenericDao dao;
	/**
	 * 添加
	 */
	public void insertSupportInfo(SupportInfo supportInfo){
		if(UtilValidate.isEmpty(supportInfo.getId())) {
            supportInfo.setId(UUIDGenerator.getId());
        }
		logger.debug("insert {}",supportInfo);
		dao.insert(supportInfo);
	}

	/**
	 * 动态更新
	 */	
	public int updateSupportInfoByPKSelective(SupportInfo supportInfo){
		logger.debug("updateByPrimaryKeySelective {}",supportInfo);
		return dao.update(SupportInfo.class.getName()+".updateByPrimaryKeySelective", supportInfo);
	}
	/**
	 * 更新
	 */
	public int updateSupportInfo(SupportInfo supportInfo){
		logger.debug("update {}",supportInfo);
		return dao.update(supportInfo);
	}
	
	
	public SupportInfo editSupportInfoById(String id){
		if(UtilValidate.isEmpty(id)) {
            return null;
        }
		logger.debug("editById {}",id);
		return (SupportInfo)dao.queryForObject(SupportInfo.class.getName()+".edit", id);
	}
	/**
	 * 根据ID查找 SupportInfo对象
	 */
	public SupportInfoDTO findSupportInfoById(String id){
		if(UtilValidate.isEmpty(id)) {
            return null;
        }
		logger.debug("findById {}",id);
		return (SupportInfoDTO) dao.get(SupportInfo.class, id);
	}

	/**
	 * 通过流程实例ID 获取，包含当前环节信息，因为并行流程的存在，所有必须传递 当前环节处理用户的ID
	 * @param processInstId
	 * @return
	 */
	public SupportInfoDTO findSupportInfoByProcessInstId(String processInstId,String curUserId){
		if(UtilValidate.isEmpty(processInstId) || UtilValidate.isEmpty(curUserId)) {
			return null;
		}
		logger.debug("findSupportInfoByProcessInstId {}",processInstId);
		Map<String,Object> params = new HashMap<>();
		params.put("processInstId",processInstId);
		params.put("curUserId",curUserId);
		return (SupportInfoDTO) dao.queryForObject(SupportInfo.class.getName()+".findSupportInfoByProcessInstId", params);
	}


	/**
	 * 删除SupportInfo对象
	 */
	public int removeSupportInfo(String id){	
		if(UtilValidate.isNotEmpty(id)) {
            return dao.delete(SupportInfo.class.getName()+".delete", id);
        }
			//return dao.remove(id);
		return 0;
	}
	
	/**
	 * 详细页查询
	 */
	public Map<String,Object> findByDetail(String id){
		return (Map<String,Object>)dao.queryForObject(SupportInfo.class.getName()+".findByDetail",id);
	}
	/** 
	 * 多条件查询结果 
	 */
	public Page<SupportInfoDTO> findByMulitCondition(Map<String,Object> param , int start , int pageSize){
		logger.debug("findByMulitCondition {}",param);
		if(pageSize<0||pageSize>2000) {
            pageSize=Page.DEFAULT_PAGE_SIZE;
        }
		if(start!=-1) {
            return dao.queryPage(SupportInfo.class.getName()+".selectMulitCondition", param, start, pageSize);
        } else{
			throw new RuntimeException("参数不正确 !!");
		}
	}
	/** 
	 * 多条件查询结果不分页
	 */
	public List<?> find(Map<String,Object> map){
		logger.debug("find {}",map);
		return dao.queryForList(SupportInfo.class.getName()+".selectByMap",map);
	}

	/**
	 * 待办查询
	 * @param userId
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public Page<SupportInfoDTO> queryDaiban(String userId,int start,int pageSize){
		return dao.queryPage(SupportInfo.class.getName()+".selectDaiban",userId,start,pageSize);
	}

	/**
	 * 通过客户ID查询支撑单
	 * @param customId
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public Page<SupportInfoDTO> findSupportInfoByCustomId(String customId,int start,int pageSize){
		if (UtilValidate.isEmpty(customId)) {
			return new Page<>();
		}
		Map<String,Object> params = new HashMap<>();
		params.put("customName",customId);
		return dao.queryPage(SupportInfo.class.getName()+".selectMulitCondition", params, start, pageSize);
	}

	/**
	 * 通过项目ID获取支撑单
	 * @param projectId
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public Page<SupportInfoDTO> findSupportInfoByProjectId(String projectId,int start,int pageSize){
		if (UtilValidate.isEmpty(projectId)) {
			return new Page<>();
		}
		Map<String,Object> params = new HashMap<>();
		params.put("customGroupId",projectId);
		return dao.queryPage(SupportInfo.class.getName()+".selectMulitCondition", params, start, pageSize);
	}

}
